<template>
    <div>
        <div class="e-form">
            <BillTop  @cancel="handleClose"></BillTop>
            <div class="tabs" style="margin-bottom: 120px">
                <div class="tabs-title">基本信息</div>
                <div style="width: 100%" class="form">
                    <el-form :model="basicForm" :label-width="formLabelWidth"
                    ref="ruleForm"
                     :rules="rules"
                     class="demo-ruleForm"
                    >
                        <el-row>

                            <el-col :span="12">
                                <el-form-item label="机构：">
                                   <span>{{ basicForm.orgName }}</span>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="业务类型：" prop="businessTypeKey">
                                      <el-select v-model="basicForm.businessTypeKey"
                                        @change="changeBusinessType"
                                    >
                                        <el-option v-for="item in businessTypeArr" :key="item.id" :value="item.id"
                                        :label="item.name"
                                        ></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>

                             <el-col :span="12">
                                <el-form-item label="采购人员：">
                                     <el-input v-model="basicForm.purchaserName" readonly>
                                                <i slot="suffix" class="el-input__icon el-icon-document-copy"  @click="selectName" ></i>
                                            </el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>

                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="入库日期：">

                                            <el-date-picker
                                        value-format="yyyy-MM-dd"
                                        :clearable="false"
                                        type="date"
                                        placeholder="选择日期"
                                        v-model="basicForm.warehousingDate"
                                    ></el-date-picker>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="收料人员：">
                                     <el-input v-model="basicForm.receivingPersonnelName" readonly>
                                                <i slot="suffix" class="el-input__icon el-icon-document-copy"  @click="selectreceivingPersonnelName" ></i>
                                            </el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="备注：">
                                    <el-input
                                        type="textarea"
                                        v-model="basicForm.remarks"
                                        autocomplete="off"
                                    ></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>

                    </el-form>
                </div>
            </div>
            <div class="buttons">
                       <span class="tip"><span style="color: red; margin-right: 0">*</span>为必填项</span>
                <el-button
                    type="primary"
                    size="small"
                    style="background: #2e61d7"
                    @click="addSave"
                    >保存</el-button
                >

                <el-button size="small" @click="handleClose">取消</el-button>
            </div>
        </div>
    </div>
</template>

<script>
// import { openAppClient } from '@/utils/common'
import { mapMutations, mapState } from 'vuex'
import { selectName, TurnoverSelectInstockMaterials } from '@/api/turnover/ppw.js'
import { doChildren } from '@/api/turnover/utils.js'
export default {
    data () {
        return {
            //基础信息
            basicForm: {
                'billId': '',
                'billNo': '',
                'businessTypeKey': '',
                'businessTypeValue': '',
                'isRed': 0,
                'nullifyCreated': '',
                'nullifyCreator': '',
                'nullifyCreatorId': '',
                'nullifyDescription': '',
                'nullifyReason': '',
                'orgId': '',
                'orgName': '',
                'parentId': '',
                'purchaserId': '',
                'purchaserName': '',
                'receivingPersonnelId': '',
                'receivingPersonnelName': '',
                'remarks': '',
                'sourceId': '',
                'sourceNo': '',
                'state': 0,
                'supplierId': '',
                'supplierName': '',
                'totalQuantity': 0,
                'warehousingDate': new Date,
                'workId': '',
                'dtlList': []
            },
            formLabelWidth: '180px',
            stateArr: ['草稿', '审核中', '已审核', '已作废'],
            rules: {
                businessTypeKey: [
                    { required: true, message: '请选择业务类型', trigger: 'change' },
                ],
                sourceNo: [
                    { required: true, message: '请选择源单编号' },
                ],
                supplierName: [
                    { required: true, message: '请选择供方' },
                ],
            },
            yearNeedPlanList: [],

        }
    },
    created () {
        this.getUserInfo()
    },
    computed: {
        ...mapState({
            userInfo: state => state.userInfo,
            businessTypeArr: state=>state.turnover.turnoverData.businessTypeArr,
        }),

        orgCode () {
            return this.$store.state.userInfo.orgInfo.shortCode
        },

    },
    methods: {
        ...mapMutations(['setSelectedInfo']),

        //获取本地登录信息
        getUserInfo () {
            const userInfo = JSON.parse(window.localStorage.getItem('vuex')).userInfo.orgInfo
            this.basicForm.orgId = userInfo.orgId
            this.basicForm.orgName = userInfo.orgName
            this.basicForm.receivingPersonnelName = this.userInfo.userName
            this.basicForm.receivingPersonnelId = this.userInfo.userId

            this.basicForm.purchaserName = this.userInfo.userName
            this.basicForm.purchaserId = this.userInfo.userId
        },
        changeBusinessType () {
            this.basicForm.sourceNo = null
            this.basicForm.supplierName = null
            const obj =  this.businessTypeArr.find(item=>this.basicForm.businessTypeKey === item.id)
            this.basicForm.businessTypeValue = obj.name
        },
        // 选择人员
        async selectName () {
            const obj = await selectName()
            this.basicForm.purchaserName = obj.userName
            this.basicForm.purchaserId = obj.userId
        },
        async selectreceivingPersonnelName () {
            const obj = await selectName()
            this.basicForm.receivingPersonnelName = obj.userName
            this.basicForm.receivingPersonnelId = obj.userId
        },
        // 引入源单信息
        async clickSingle () {
            // const data =  [{
            //     'billId': null,
            //     'billNo': 'SRBGZCNZDD20220126001',
            //     'dtlId': '1486168294460231687',
            //     'id': '1486168294460231681',
            //     'materialClassId': '1500727756765982722/1500729232783503361/1500729509653704706',
            //     'materialClassName': '周转材料/型材及管材/花纹钢板',
            //     'materialId': '1500729760934457345',
            //     'materialName': '花纹钢板',
            //     'name': '四川公路桥梁建设集团有限公司',
            //     'notWarehosingQuantity': 11,
            //     'parentId': '1439990097591140358',
            //     'quantity': 12,
            //     'spec': '321',
            //     'texture': '213',
            //     'unit': '321',
            //     'isDetailed': 1,
            //     'materialClassNameArr': ['型材及管材', '花纹钢板'],
            //     'materialClassIdArr': ['1500729232783503361', '1500729509653704706']
            // }]

            const data = await TurnoverSelectInstockMaterials(this.basicForm.businessTypeKey)

            this.basicForm.sourceNo = data[0].billNo
            this.basicForm.supplierName = data[0].name
            // this.basicForm.supplierName = data[0].name
            this.basicForm.dtlList = doChildren(data, 'storage', null,  this.basicForm.businessTypeKey) //选原单之后保存明细
            // 客户端弹窗预览结果
            // 参数依次为: 打开页面地址、标题、回调函数
            // openAppClient('/showSelectedData', '选择结果', function () {
            //     // alert('关闭后回调')
            // })
        },
        //基本信息保存
        addSave () {
            this.$refs.ruleForm.validate(valid => {
                if (valid) {
                    this.$Ajax
                        .httpPost({
                            url: '/turnover/warehousing/save',
                            params: this.basicForm
                        })
                        .then(res => {

                            this.clientPop('info', '保存成功!是否进行下一步操作？', ()=>{
                                this.$router.push({
                                    path: '/revolveStorageEdit',
                                    query: {
                                        billid: res,
                                        name: 'detailed'
                                    }
                                })
                            },
                            null,
                            this.handleClose
                            )
                        })
                }
            })

        },
        //关闭
        handleClose () {
            this.$router.push('/revolveStorageList')
        }
    }
}
</script>

<style lang="scss" scoped>
.buttons {
    left: 0;
}
.e-form {
    padding: 0 20px;
    .tabs-title::before {
        content: '';
        height: 22px;
        width: 8px;
        border-radius: 40px;
        background-color: #2e61d7;
        display: block;
        position: absolute;
        left: 20px;
        margin-right: 20px;
    }
}
.e-table {
    min-height: auto;
    background: #fff;
}
.separ {
    width: 30px;
    height: 40px;
    line-height: 18px;
    text-align: center;
}
.upload {
    margin: 20px auto;
    display: flex;
    justify-content: center;
    text-align: center;
}

.upload-demo {
    display: flex;
    justify-content: center;
    align-items: center;
}

::v-deep.el-input--suffix .el-input__inner {
    padding-right: 5px;
}
::v-deep.el-table .cell {
    text-align: center;
}
</style>
